package zqkcom.atguigu.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter extends HttpFilter{

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        if (user == null) {

            //未登录，使用请求域+转发的方式跳转到登录页面，并提示用户信息
            request.setAttribute("errorMsg", "订单功能请先登录");
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        } else {
            //已登录，放行
            chain.doFilter(request,response);
        }
    }


}
